<?php
$dsn = "mysql:host=localhost;dbname=form_data;charset=utf8";
$user = 'root';
$pass = '123';
$pdo = new PDO($dsn,$user,$pass);
//pdo类的数据库连接方式
function val($arr){
    $str = "";
    foreach ($arr as $value){
        //遍历
        $str.=$value;//连接字符
     }
     return $str;
}
$_POST['why'] = val($_POST['why']);
$_POST['damage'] = val($_POST['damage']);
$_POST['reason'] = val($_POST['reason']);
//接受传入的参数

$query = "insert form_data(sex,age,during,time,type,why,days,reason,health,state,damage)
values('{$_POST['sex']}','{$_POST['age']}','{$_POST['during']}',
'{$_POST['time']}','{$_POST['type']}','{$_POST['why']}',
'{$_POST['days']}','{$_POST['reason']}','{$_POST['health']}',
'{$_POST['state']}','{$_POST['damage']}')";
//SQL语句到数据库中插入数据
$row = $pdo->exec($query);
//操作
$id = $pdo->lastInsertId();
//获取插入成功后的id
if($id>0){
    $data = array ('msg'=>"ok",'status'=>1,'id'=>$id);
}else{
    $data = array ('msg'=>$pdo->errorInfo()[2],'status'=>0);
}
echo json_encode($data);
?>
